package com.wgts.wgts.mvp.view.activity.login;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextPaint;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.LinkMovementMethod;
import android.text.method.PasswordTransformationMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.base.BaseActivity;
import com.wgts.wgts.control.toast.MoHuToast;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerBindPhoneActivityComponent;
import com.wgts.wgts.dagger.module.BindPhoneActivityModule;
import com.wgts.wgts.mvp.presenter.IBindPhoneActivityPersenter;
import com.wgts.wgts.mvp.presenter.imp.BindPhoneActivityPersenter;
import com.wgts.wgts.mvp.view.IBindPhoneActivityView;
import com.wgts.wgts.mvp.view.activity.webactivity.WebActivity;
import com.wgts.wgts.netmodle.NetWorkConstant;
import com.wgts.wgts.netmodle.resp.LoginEntity;
import com.wgts.wgts.netmodle.resp.ThridPreBindEntity;
import com.wgts.wgts.thirdParty.push.PushTools;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/*
 * 注册页面
 *
 * */

public class BindPhoneActivity extends BaseActivity implements IBindPhoneActivityView {

    @Inject
    IBindPhoneActivityPersenter persenter;
    @BindView(R.id.back_image)
    ImageView backImage;
    @BindView(R.id.phone_et)
    EditText phoneEt;
    @BindView(R.id.phone_ly)
    RelativeLayout phoneLy;
    @BindView(R.id.code_et)
    EditText codeEt;
    @BindView(R.id.get_code)
    TextView getCode;
    @BindView(R.id.code_ly)
    RelativeLayout codeLy;
    @BindView(R.id.invite_et)
    EditText inviteEt;
    @BindView(R.id.invite_ly)
    RelativeLayout inviteLy;
    @BindView(R.id.password_et)
    EditText passwordEt;
    @BindView(R.id.show_password)
    ImageView showPassword;
    @BindView(R.id.password_ly)
    RelativeLayout passwordLy;
    @BindView(R.id.register_ly)
    RelativeLayout registerLy;
    @BindView(R.id.rule_text)
    TextView ruleText;

    public static final int BIND_PHONE_RESULT = 100;
    public static final String PHONE = "phone";
    public static final String PASSWORD = "password";

    private boolean isShow = false;

    private String userId;
    private String unionid;

    @Override
    public void init() {
        DaggerBindPhoneActivityComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .bindPhoneActivityModule(new BindPhoneActivityModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void onCreate(Bundle saveIntanceBundle) {
        super.onCreate(saveIntanceBundle);
        setContentView(R.layout.wgts_register_activity);
        ButterKnife.bind(this);
        initView();
    }

    private void initView(){
        String rule = "温馨提示：点击完成注册即表示您已仔细阅读并同意《用户协议》 《隐私协议》";
        Spannable span = new SpannableString(rule);
        span.setSpan(new textClick(1),23, 29, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        span.setSpan(new textClick(2), 30, 36, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        ruleText.setText(span);
        ruleText.setMovementMethod(LinkMovementMethod.getInstance());
    }

    @Override
    public void loginResult(ThridPreBindEntity thridPreBindEntity) {
        dialogDismiss();
    }

    @Override
    public void countDownTimeOver() {
        getCode.setEnabled(true);
        getCode.setText("重新获取");
        persenter.removeCountDownTimeListener();
    }

    @Override
    public void countDowmTime(final int time) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                getCode.setText(String.valueOf(time) + "s");
            }
        });
    }

    @Override
    public void sendSmsResult() {
        dialogDismiss();
        MoHuToast.showToast("发送成功");
        persenter.addCountDownTimeListener();
    }

    @Override
    public void registerResult(LoginEntity entity) {
        dialogDismiss();
        //设置别名
        PushTools.getInstance().setAlias(this, entity.alia);

        Intent intent = new Intent();
        intent.putExtra(PHONE, phoneEt.getText().toString().trim());
        intent.putExtra(PASSWORD, passwordEt.getText().toString().trim());
        setResult(BIND_PHONE_RESULT, intent);
        finish();
    }

    @Override
    public void onError(int status, String msg) {
        if (status == BindPhoneActivityPersenter.SMS_ERROR){
            getCode.setEnabled(true);
            getCode.setText("获取验证码");
            dialogDismiss();
            MoHuToast.showToast(msg);
            return;
        }
        dialogDismiss();
        MoHuToast.showToast(msg);
    }

    private void sendSMS() {
        if (phoneEt.getText().toString() != null) {

            if (phoneEt.getText().toString().trim().length() != 11) {
                MoHuToast.showToast("请输入正确的手机号");
                return;
            }

        } else {
            MoHuToast.showToast("请输入正确的手机号");
            return;
        }
        getCode.setText("正在发送");
        getCode.setEnabled(false);
        showDialog();
        persenter.sendSms(phoneEt.getText().toString().trim(), "REGISTER");
    }

    private void register() {
        if (phoneEt.getText().toString() != null) {
            if (phoneEt.getText().toString().trim().length() != 11) {
                MoHuToast.showToast("请输入正确的手机号");
                return;
            }
        } else {
            MoHuToast.showToast("请输入正确的手机号");
            return;
        }
        if (codeEt.getText().toString() == null || codeEt.getText().toString().equals("")){
            MoHuToast.showToast("请输入正确的验证码");
            return;
        }else {
            if (codeEt.getText().toString().trim().length() != 4) {
                MoHuToast.showToast("请输入正确的验证码");
                return;
            }
        }
        if (inviteEt.getText().toString().equals("") || inviteEt.getText().toString() == null){
            MoHuToast.showToast("请输入正确的邀请码");
            return;
        }
        if(passwordEt.getText().toString().trim().length() < 6 || passwordEt.getText().toString().trim().length() > 16){
            MoHuToast.showToast("请输入正确的密码");
            return;
        }
        showDialog();
        persenter.register(phoneEt.getText().toString().trim(), passwordEt.getText().toString().trim()
        , codeEt.getText().toString().trim(), inviteEt.getText().toString().trim());
    }


    @OnClick({R.id.back_image, R.id.get_code, R.id.show_password, R.id.register_ly})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_image:
                finish();
                break;
            case R.id.get_code:
                sendSMS();
                break;
            case R.id.show_password:
                if (isShow){
                    showPassword.setImageResource(R.mipmap.icon_hide);
                    passwordEt.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    passwordEt.setSelection(passwordEt.getText().length());
                    isShow = false;
                }else {
                    showPassword.setImageResource(R.mipmap.icon_display);
                    passwordEt.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    passwordEt.setSelection(passwordEt.getText().length());
                    isShow = true;
                }
                break;
            case R.id.register_ly:
                register();
                break;
        }
    }

    class textClick extends ClickableSpan{
        private int type;
        textClick(int type){
            this.type = type;
        }
        @Override
        public void onClick(View widget) {
            switch (type){
                case 1:
                    Intent intent = new Intent(BindPhoneActivity.this, WebActivity.class);
                    intent.putExtra(WebActivity.TITLE, "晒友盟注册协议");
                    intent.putExtra(WebActivity.ACTION_URL, NetWorkConstant.BASE_AGREEMENT + "/registration_agreement.html");
                    startActivity(intent);
                    break;
                case 2:
                    Intent intent1 = new Intent(BindPhoneActivity.this, WebActivity.class);
                    intent1.putExtra(WebActivity.TITLE, "隐私协议");
                    intent1.putExtra(WebActivity.ACTION_URL, NetWorkConstant.BASE_AGREEMENT + "/privacy_policy.html");
                    startActivity(intent1);
                    break;
            }
        }
        @Override
        public void updateDrawState(TextPaint ds) {
            ds.setColor(ContextCompat.getColor(BindPhoneActivity.this, R.color.bulue_copy));
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        persenter.removeCountDownTimeListener();
    }

}
